package com.xydtech.ecard.custgroup.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.common.utils.ShiroUtils;
import com.xydtech.common.utils.poi.ExcelUtil;
import com.xydtech.ecard.custgroup.domain.EcardCustomerGroupMerchant;
import com.xydtech.ecard.custgroup.service.IEcardCustomerGroupMerchantService;
import com.xydtech.ecard.merchant.domain.EcardReleaseRecord;
import com.xydtech.ecard.merchant.service.IEcardReleaseRecordService;
import com.xydtech.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子名片行业客户Controller
 *
 * @author yuzhiguo
 * @date 2024-12-03 下午4:49
 */
@Controller
@RequestMapping("/ecard/groupMerchant")
public class EcardCustomerGroupMerchantController  extends BaseController
{
    private String prefix = "ecard/custgroup/groupMerchant";

    @Autowired
    private IEcardCustomerGroupMerchantService ecardCustomerGroupMerchantService;
    @Autowired
    private IEcardReleaseRecordService ecardReleaseRecordService;
    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("ecard:groupMerchant:view")
    @GetMapping()
    public String groupMerchant()
    {
        return prefix + "/groupMerchant";
    }

    /**
     * 查询电子名片行业客户列表
     */
    @RequiresPermissions("ecard:groupMerchant:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EcardCustomerGroupMerchant ecardCustomerGroupMerchant)
    {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardCustomerGroupMerchant.setDictDeptId(sysDept.getDeptId());
        startPage();
        List<EcardCustomerGroupMerchant> list = ecardCustomerGroupMerchantService.selectEcardCustomerGroupMerchantList(ecardCustomerGroupMerchant);
        return getDataTable(list);
    }

    /**
     * 导出电子名片行业客户列表
     */
    @RequiresPermissions("ecard:groupMerchant:export")
    @Log(title = "电子名片行业客户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EcardCustomerGroupMerchant ecardCustomerGroupMerchant)
    {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(ShiroUtils.getSysUser().getDeptId());
        ecardCustomerGroupMerchant.setDictDeptId(sysDept.getDeptId());
        List<EcardCustomerGroupMerchant> list = ecardCustomerGroupMerchantService.selectEcardCustomerGroupMerchantList(ecardCustomerGroupMerchant);
        ExcelUtil<EcardCustomerGroupMerchant> util = new ExcelUtil<EcardCustomerGroupMerchant>(EcardCustomerGroupMerchant.class);
        return util.exportExcel(list, "电子名片行业客户数据");
    }

    /**
     * 跳转行业客户个人信息列表
     * @return
     */
    @RequiresPermissions("ecard:groupPerMerchant:view")
    @GetMapping("/groupPerMerchant/{merchantId}")
    public String groupPerMerchant(@PathVariable("merchantId") String merchantId, ModelMap mmap)
    {
        mmap.put("merchantId", merchantId);
        return prefix + "/groupPerMerchant";
    }

    /**
     * 跳转行业客户商户信息列表
     * @return
     */
    @RequiresPermissions("ecard:groupMerMerchant:view")
    @GetMapping("/groupMerMerchant/{merchantId}")
    public String groupMerMerchant(@PathVariable("merchantId") String merchantId, ModelMap mmap)
    {
        mmap.put("merchantId", merchantId);
        return prefix + "/groupMerMerchant";
    }

    /**
     * 跳转行业客户企业信息列表
     * @return
     */
    @RequiresPermissions("ecard:groupEntMerchant:view")
    @GetMapping("/groupEntMerchant/{merchantId}")
    public String groupEntMerchant(@PathVariable("merchantId") String merchantId, ModelMap mmap)
    {
        mmap.put("merchantId", merchantId);
        return prefix + "/groupEntMerchant";
    }

    /**
     * 跳转行业客户新型农业主体信息列表
     * @return
     */
    @RequiresPermissions("ecard:groupAgrMerchant:view")
    @GetMapping("/groupAgrMerchant/{merchantId}")
    public String groupAgrMerchant(@PathVariable("merchantId") String merchantId, ModelMap mmap)
    {
        mmap.put("merchantId", merchantId);
        return prefix + "/groupAgrMerchant";
    }

    /**
     * 选择转交的客户经理
     */
    @RequiresPermissions("ecard:groupMerchant:handOver")
    @GetMapping("/handOver/{ids}")
    public String choiceUserByHand(@PathVariable("ids") String ids, ModelMap mmap) {
        mmap.put("ids", ids);
        return prefix + "/choiceUser";
    }

    /**
     * 行业客户信息转交
     */
    @Log(title = "行业客户信息转交客户经理", businessType = BusinessType.UPDATE)
    @PostMapping("/allocation")
    @ResponseBody
    public AjaxResult allocation(Long registId,Long deptId, Long[] ids, Long userId) {
        /**
         * 行业客户转交时调用对应类型商户信息转交方法，方法中同步转交行业客户信息
         */
        /**查询机构*/
        //SysUser user = sysUserService.selectUserById(userId);
        if(registId == null){
            return AjaxResult.error("请选择移交人员！");
        }
        if(deptId == null){
            return AjaxResult.error("请选择移交的机构！");
        }

        //移交行业客户信息列表
        List<EcardCustomerGroupMerchant> ecardCustomerGroupMerchantList = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            //修改管户人id和创建人机构id
            EcardCustomerGroupMerchant customerGroupMerchant = new EcardCustomerGroupMerchant();
            customerGroupMerchant.setId(ids[i]);
            customerGroupMerchant.setManagementPersonnelId(registId);
            customerGroupMerchant.setDeptId(deptId);
            SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
            customerGroupMerchant.setCorpId(sysDept.getDeptId());//法人机构id
            ecardCustomerGroupMerchantList.add(customerGroupMerchant);
        }
        return toAjax(ecardCustomerGroupMerchantService.allocationEcardGroupMerchantInfo(ecardCustomerGroupMerchantList,registId, userId));
    }

    /**
     * 释放行业客户
     *
     * @author yuzhiguo
     * @date 2024/12/4 下午4:30
     */
    @RequiresPermissions("ecard:groupMerchant:handOver")
    @Log(title = "释放行业客户", businessType = BusinessType.UPDATE)
    @PostMapping("/release")
    @ResponseBody
    public AjaxResult release(Long[] ids) {
        /**
         * 行业客户释放时调用对应类型商户信息释放方法，方法中同步释放行业客户信息
         */
        String salt = getSalt();//盐值
        //行业客户列表
        List<EcardCustomerGroupMerchant> ecardCustomerGroupMerchantList = new ArrayList<>();
        Long userId = ShiroUtils.getSysUser().getUserId();
        //新增释放记录
        List<EcardReleaseRecord> ecardReleaseRecordList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            EcardCustomerGroupMerchant groupMerchant = ecardCustomerGroupMerchantService.selectEcardCustomerGroupMerchantById(ids[i]);//查询行业客户信息

            //修改创建人id置为null
            EcardCustomerGroupMerchant ecardCustomerGroupMerchant = new EcardCustomerGroupMerchant();
            ecardCustomerGroupMerchant.setId(ids[i]);
            ecardCustomerGroupMerchant.setManagementPersonnelId(null);
            ecardCustomerGroupMerchant.setMerchantId(groupMerchant.getMerchantId());
            ecardCustomerGroupMerchant.setMerchantType(groupMerchant.getMerchantType());
            ecardCustomerGroupMerchantList.add(ecardCustomerGroupMerchant);

            //新增释放记录
            EcardReleaseRecord ecardReleaseRecord = new EcardReleaseRecord();
            ecardReleaseRecord.setReleaseId(userId);//释放人用户id
            ecardReleaseRecord.setMerchantId(groupMerchant.getMerchantId());//商户id
            ecardReleaseRecord.setBelongRegistId(groupMerchant.getManagementPersonnelId());//归属人注册id
            ecardReleaseRecord.setMerchantType(groupMerchant.getMerchantType());//商户类型：1-个人，2-商户，3-企业，4-新型农业主体
            ecardReleaseRecordList.add(ecardReleaseRecord);
        }
        ecardReleaseRecordService.batchInsertEcardReleaseRecord(ecardReleaseRecordList);//批量新增释放记录
        int i = ecardCustomerGroupMerchantService.batchReleaseEcardCustomerGroupMerchant(ecardCustomerGroupMerchantList);
        return toAjax(i);
    }

    /**
     * 启用/停用新型农业主体
     *
     * @author yuzhiguo
     * @date 2024/8/16 下午3:32
     */
    @RequiresPermissions("ecard:groupMerchant:switchOver")
    @Log(title = "启用/停用行业客户", businessType = BusinessType.UPDATE)
    @PostMapping("/switchOver")
    @ResponseBody
    public AjaxResult switchOver(Long[] ids, Integer state) {
        /**
         * 行业客户启用/停用时调用对应类型商户信息启用/停用方法，方法中同步启用/停用行业客户信息
         */
        //行业客户列表
        List<EcardCustomerGroupMerchant> ecardCustomerGroupMerchantList = new ArrayList<>();

        for (int i = 0; i < ids.length; i++) {
            EcardCustomerGroupMerchant groupMerchant = ecardCustomerGroupMerchantService.selectEcardCustomerGroupMerchantById(ids[i]);//查询行业客户信息
            //修改行业客户状态
            EcardCustomerGroupMerchant customerGroupMerchant = new EcardCustomerGroupMerchant();
            customerGroupMerchant.setId(ids[i]);
            customerGroupMerchant.setState(state);
            customerGroupMerchant.setMerchantId(groupMerchant.getMerchantId());
            customerGroupMerchant.setMerchantType(groupMerchant.getMerchantType());
            ecardCustomerGroupMerchantList.add(customerGroupMerchant);
        }
        int i = ecardCustomerGroupMerchantService.batchSwitchOverEcardCustomerGroupMerchantInfo(ecardCustomerGroupMerchantList);
        return toAjax(i);
    }

    /**
     * 新增电子名片行业客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存电子名片行业客户
     */
    @RequiresPermissions("ecard:groupMerchant:add")
    @Log(title = "电子名片行业客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EcardCustomerGroupMerchant ecardCustomerGroupMerchant)
    {
        return toAjax(ecardCustomerGroupMerchantService.insertEcardCustomerGroupMerchant(ecardCustomerGroupMerchant));
    }

    /**
     * 修改电子名片行业客户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EcardCustomerGroupMerchant ecardCustomerGroupMerchant = ecardCustomerGroupMerchantService.selectEcardCustomerGroupMerchantById(id);
        mmap.put("ecardCustomerGroupMerchant", ecardCustomerGroupMerchant);
        return prefix + "/edit";
    }

    /**
     * 修改保存电子名片行业客户
     */
    @RequiresPermissions("ecard:groupMerchant:edit")
    @Log(title = "电子名片行业客户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EcardCustomerGroupMerchant ecardCustomerGroupMerchant)
    {
        return toAjax(ecardCustomerGroupMerchantService.updateEcardCustomerGroupMerchant(ecardCustomerGroupMerchant));
    }

    /**
     * 删除电子名片行业客户
     */
    @RequiresPermissions("ecard:groupMerchant:remove")
    @Log(title = "电子名片行业客户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ecardCustomerGroupMerchantService.deleteEcardCustomerGroupMerchantByIds(ids));
    }
}
